﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevOnServer
{
    public class CRespawner : CTimed
    {
        public CRespawner(CWorld world, ObjType objType, int modelId, float heightPoint, int count, int time, int aggro, CVec2Di minPoint, CVec2Di maxPoint)
        {
            m_world = world;
            m_objType = objType;
            HeightPos = heightPoint;
            m_objMaxCount = count;
            m_respawnTime = time;
            m_aggroChance = aggro;
            MinPos = minPoint;
            MaxPos = maxPoint;

            if (objType == ObjType.Drop)
            {
                m_itemProp = m_world.Server.DataManager.ItemProperties[modelId];
            }
            else
            {
                m_moverProp = m_world.Server.DataManager.MoverProperties[modelId];
            }

            CWorldObject obj;
            while (m_objects.Count < m_objMaxCount)
            {
                if (m_objType == ObjType.Drop)
                {
                    obj = m_world.createDropObject(m_itemProp,
                        new CVec3Df((float)CRandom.random(MinPos.X, MaxPos.X), 0.0f, (float)CRandom.random(MinPos.Y, MaxPos.Y)),
                        null);
                }
                else
                {
                    obj = m_world.createMonsterObject(m_moverProp,
                        new CVec3Df((float)CRandom.random(MinPos.X, MaxPos.X), HeightPos, (float)CRandom.random(MinPos.Y, MaxPos.Y)));

                    if (CRandom.random(1, m_objMaxCount) == m_aggroChance)
                        (obj as CMonster).Aggro = true;
                }

                m_objects.Add(obj);
                obj.Respawner = this;
            }
        }

        public void removeObj(CWorldObject obj)
        {
            m_objects.Remove(obj);
        }

        public override void update()
        {
            if (m_objects.Count < m_objMaxCount)
            {
                if (m_nextRespawn == 0)
                {
                    m_nextRespawn = s_time + m_respawnTime;
                }
                else
                {
                    while (m_objects.Count < m_objMaxCount)
                    {
                        if (s_time > m_nextRespawn)
                        {
                            CWorldObject obj;

                            if (m_objType == ObjType.Drop)
                            {
                                obj = m_world.createDropObject(m_itemProp,
                                    new CVec3Df((float)CRandom.random(MinPos.X, MaxPos.X), 0.0f, (float)CRandom.random(MinPos.Y, MaxPos.Y)),
                                    null);
                            }
                            else
                            {
                                obj = m_world.createMonsterObject(m_moverProp,
                                    new CVec3Df((float)CRandom.random(MinPos.X, MaxPos.X), HeightPos, (float)CRandom.random(MinPos.Y, MaxPos.Y)));
                            }

                            m_objects.Add(obj);
                            obj.Respawner = this;

                            m_nextRespawn += m_respawnTime;
                        }
                        else
                        {
                            return;
                        }
                    }

                    m_nextRespawn = 0;
                }
            }
        }

        private CWorld m_world;

        private List<CWorldObject> m_objects = new List<CWorldObject>();

        private ObjType m_objType;

        private int m_respawnTime;

        private int m_aggroChance;

        private CItemProp m_itemProp;

        private CMoverProp m_moverProp;

        private int m_objMaxCount;

        private int m_nextRespawn;

        public CVec2Di MinPos;

        public CVec2Di MaxPos;

        public float HeightPos;
    }
}